package jdly.water.api.service.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import jdly.water.api.common.service.R;
import jdly.water.api.dao.base.BuildingMeterDao;
import jdly.water.api.dao.base.CollectorDao;
import jdly.water.api.dao.base.MeterDao;
import jdly.water.api.model.PageModel;
import jdly.water.api.util.PageUtil;
import jdly.water.entity.base.BuildingMeter;
import jdly.water.entity.base.Meter;
import jdly.water.entity.base.TCollector;
import jodd.util.StringUtil;

@Service
public class BaseService {
	@Autowired
	private BuildingMeterDao buildingMeterDao;
	@Autowired
	private MeterDao meterDao;
	@Autowired
	private CollectorDao collectorDao;

	
	public R getMeter(String id) {
		R r = R.ok();
		Meter meter = meterDao.findOne(id);
		r.put("data", meter);
		return r;
	}
	
	public R updateMeter(String meternumber,String id) {
		R r = R.ok();
		try {
			Meter meter = meterDao.findOne(id);
			meter.setMeterNumber(meternumber);
			meterDao.saveAndFlush(meter);
		}catch(Exception e) {
			r =R.error();
			e.printStackTrace();
		}
		return r ;
	}
	
	public R getCollector(String id) {
		R r = R.ok();
		TCollector collector = collectorDao.findOne(id);
		r.put("data", collector);
		return r;
	}
	
	public R updateCollector(String dtutel,String id) {
		R r = R.ok();
		try {
			TCollector collector = collectorDao.findOne(id);
			collector.setDtuTel(dtutel);
			collectorDao.saveAndFlush(collector);
		}catch(Exception e) {
			r =R.error();
			e.printStackTrace();
		}
		return r ;
	}
	
	public R getBm(String id) {
		R r = R.ok();
		BuildingMeter meter = buildingMeterDao.findOne(id);
		r.put("data", meter);
		return r;
	}
	
	public R updateBm(String meternumber,String id) {
		R r = R.ok();
		try {
			BuildingMeter meter= buildingMeterDao.findOne(id);
			meter.setMeterNumber(meternumber);
			buildingMeterDao.saveAndFlush(meter);
		}catch(Exception e) {
			r =R.error();
			e.printStackTrace();
		}
		return r ;
	}
	
	public R userlist(PageModel page, Map<String,Object> parammap) {
		R r = R.ok();
		Specification<Meter> querySpecifi = new Specification<Meter>() {
			List<Predicate> plst = new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<Meter> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
			
				Set<String> klst =  parammap.keySet();
				for(String k:klst) {
					if(parammap.get(k)!=null&&StringUtil.isNotEmpty(parammap.get(k).toString())) {
						if(parammap.get(k) instanceof String) {
							plst.add(cb.like(root.get(k),"%"+parammap.get(k).toString()+"%"));
						}else {
							plst.add(cb.equal(root.get(k),parammap.get(k)));
						}
					}
				}
				Predicate[] p = new Predicate[plst.size()];
				return cb.and(plst.toArray(p));
			}
		};
		Page<Meter> data = meterDao.findAll(querySpecifi, PageUtil.toPageable(page));
//		long count = meterDao.count(querySpecifi);
//		r.put("count", count);
		r.put("datalist", data.getContent());
		return r;
	}
	public R dongbiao(PageModel page, String search) {
		R r = R.ok();
		Specification<BuildingMeter> querySpecifi = new Specification<BuildingMeter>() {
			List<Predicate> plst = new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<BuildingMeter> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				if(!StringUtil.isEmpty(search)) {
					plst.add(cb.like(root.get("meterNumber"),"%"+search+"%"));
				}
				plst.add(cb.like(root.get("meterModel"),"栋表"));
				Predicate[] p = new Predicate[plst.size()];
				return cb.and(plst.toArray(p));
			}
		};
		Page<BuildingMeter> datalist = buildingMeterDao.findAll(querySpecifi, PageUtil.toPageable(page));
//		long count = buildingMeterDao.count(querySpecifi);
//		r.put("count", count);
		r.put("datalist", datalist.getContent());
		return r;
	}
	public R zongbiao(PageModel page, String search) {
		R r = R.ok();
		Specification<BuildingMeter> querySpecifi = new Specification<BuildingMeter>() {
			List<Predicate> plst = new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<BuildingMeter> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				if(!StringUtil.isEmpty(search)) {
					plst.add(cb.like(root.get("meterNumber"),"%"+search+"%"));
				}
				plst.add(cb.like(root.get("meterModel"),"总表"));
				Predicate[] p = new Predicate[plst.size()];
				return cb.and(plst.toArray(p));
			}
		};
		Page<BuildingMeter> datalist = buildingMeterDao.findAll(querySpecifi, PageUtil.toPageable(page));
//		long count = buildingMeterDao.count(querySpecifi);
//		r.put("count", count);
		r.put("datalist", datalist.getContent());
		return r;
	}
	
	public R dtu(String search) {
		R r = R.ok();
		if(StringUtil.isEmpty(search)) {
			List<TCollector> clist = this.collectorDao.findAll();
			r.put("datalist", clist);
			r.put("count", clist.size());
		}else {
			List<TCollector> clist = this.collectorDao.findByTel(search);
			r.put("datalist", clist);
			r.put("count", clist.size());
		}
		return r;
	}

}
